/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bots;

import jabachat.ChatUI;
import jabachat.NullChatClient;
import java.net.MalformedURLException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fgssdfs
 */
public class AngryBot implements ChatUI {

    private NullChatClient chat;
    private Thread t;
    int online;
    boolean firstMessage = true;
    long millis;
    long millisN;
    int fastRelogin = 0;

    AngryBot() {
    }

    public void setOn() {
        try {
            chat = new NullChatClient(this, "http://0chan.ru/0chat", "0chan.ru");
            t = new Thread(chat);
            t.start();
            System.out.println("Started");
        } catch (MalformedURLException ex) {
            Logger.getLogger(AngryBot.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    @Override
    public void message(String reply, int type) {
        switch (type) {
            case 2: //online
                if (firstMessage) {
                    online = Integer.valueOf(reply.trim());
                    chat.send("**<HungryBot>** К вам пришёл я, поприветствуем!");
                    firstMessage = false;
                    millis = System.currentTimeMillis();
                } else {
                    millis = millisN;
                    millisN = System.currentTimeMillis();
                    if (millisN - millis < 1000) {
                        fastRelogin++;

                    }

                    if (fastRelogin == 3) {
                        chat.send("**<HungryBot>** Не стоит этого делать, дорогой.");
                        break;
                    } else if (fastRelogin > 3) {
                        break;
                    }


                    if (Integer.valueOf(reply.trim()) > online) {
                        chat.send("**<HungryBot>** К нам пришел еще один мудак, поприветствуем!");
                        online = Integer.valueOf(reply.trim());
                    } else if (Integer.valueOf(reply.trim()) < online) {
                        chat.send("**<HungryBot>** От нас ушел какой-то мудак - честь ему и хвала!");
                        online = Integer.valueOf(reply.trim());

                    }


                }

                fastRelogin = 0;
                break;
            case 5: //captcha
                System.out.println("captcha: \n" + reply);
                Scanner conin = new Scanner(System.in);
                String str = conin.next();
                chat.send(str);
                System.out.println("sended: " + str);

                break;

//                        default: //captcha
//                System.out.println("default:" + reply);
//                break;

        }



    }

    public static void main(String[] args) {

        AngryBot a = new AngryBot();
        a.setOn();
        System.currentTimeMillis();
    }
}
